﻿using System.Collections.ObjectModel;
using System.Windows.Controls;
using BomDiffAnalyst.Events;
using BomDiffAnalyst.Views;
using log.Nlog.ILogger;
using Module.Common.Model;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Navigation.Regions;

namespace BomDiffAnalyst.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IEventAggregator _ea;
        private static readonly LeftNavigationViewModel s_instance;

        private readonly IContainerExtension _container;
        private readonly IRegionManager _regionManager;
        private readonly IDialogService _dialogService;

        private readonly IModuleCatalog _moduleCatalog;
        private ObservableCollection<ModuleInfo> _moduleInfos;



        private int _selectedIndex;
        private readonly ILoggerService _logger;
        private string _title = "Prism Unity Application";


        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private ObservableCollection<string> _messages;
        public ObservableCollection<string> Messages
        {
            get { return _messages; }
            set { SetProperty(ref _messages, value); }
        }

        private ObservableCollection<ModuleDescriptor> _dataService = new ObservableCollection<ModuleDescriptor>();
        public ObservableCollection<ModuleDescriptor> DataService
        {
            get => _dataService;
            set => SetProperty(ref _dataService, value);
        }


        public DelegateCommand<SelectionChangedEventArgs> SelectedCommand { get; private set; }

        private ObservableCollection<ModuleDescriptor> _moduleCollection = null;


        public DelegateCommand<string> NavigateCommand { get; private set; }

        public ObservableCollection<ModuleInfo> ModuleInfos
        {
            get { return _moduleInfos; }
            set { _moduleInfos = value; RaisePropertyChanged(); }
        }


        public MainWindowViewModel(
                                    IEventAggregator ea,
                                    IRegionManager regionManager,
                                    IDialogService dialogService,
                                    IModuleCatalog moduleCatalog,
                                    ILoggerService logger)
                                    
        {
            //throw new Exception("hello world.");


            _logger = logger;
            _logger.LogInfo("测试log,应用程序启动");


            _regionManager = regionManager;
            _dialogService = dialogService;
            _moduleCatalog = moduleCatalog;
            _ea = ea;


            //将LeftNavigationView 显示到主界面
            _regionManager.RegisterViewWithRegion("MainWindow.LeftRegion", typeof(LeftNavigation));
            _regionManager.RegisterViewWithRegion("MainWindow.TopRegion", typeof(TopNavigation));
            //_regionManager.RegisterViewWithRegion("MainWindow.pop", typeof(UserControl2));

            //注册导航
            //NavigateCommand = new DelegateCommand<string>(Navigate);

            Messages = new ObservableCollection<string>();


            //接受消息
            _ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived);

        }



        private void MessageReceived(string message)
        {
            Messages.Add(message);
        }



        public int SelectedIndex
        {
            get => _selectedIndex;
            set => SetProperty(ref _selectedIndex, value);
        }



        //private void Navigate(string navigatePath)
        //{
        //    if (navigatePath != null)
        //        _regionManager.RequestNavigate("MainWindow.ModuleContentRegion", navigatePath);
        //}

        //private void NavigationComplete(NavigationResult result)
        //{
        //    MessageBox.Show(string.Format("Navigation to {0} complete. ", result.Context.Uri));
        //}
    }
}
